package com.lt.integration.getui;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.lt.integration.getui.dto.ConfigDTO;
import com.lt.integration.getui.env.ApiContext;

import java.util.UUID;

/**
 * 封装发送
 * @ author     :  Dragon
 * @ Created in :  2022年02月14日 14:16
 */
public class PushUtils {

    public static PushApi before(ConfigDTO dto) {
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        ApiContext apiContext = ApiContext.build(dto);
        apiContext.configuration.setAnalyseStableDomainInterval(500);
        apiContext.configuration.setOpenCheckHealthDataSwitch(true);
        apiContext.configuration.setCheckHealthInterval(500);
        apiContext.configuration.setOpenAnalyseStableDomainSwitch(false);  //关闭
        ApiHelper apiHelper = ApiHelper.build(apiContext.configuration);
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        return pushApi;
    }

    /**
     * 调用推送
     * @param dto 配置测试
     * @param notification GT的通知
     * @return
     */
    public static ApiResult pushAll(ConfigDTO dto, GTNotification notification) {
        PushApi pushApi = before(dto);
        PushDTO<String> pushDTO = new PushDTO<String>();
        pushDTO.setRequestId(UUID.randomUUID().toString().substring(0, 16));
        pushDTO.setGroupName("g-name");
        Settings settings = new Settings();
        settings.setTtl(3600000);
        pushDTO.setSettings(settings);
        pushDTO.setAudience("all");
        PushMessage pushMessage = new PushMessage();
        pushMessage.setNotification(notification);
        pushDTO.setPushMessage(pushMessage);
        ApiResult apiResult = pushApi.pushAll(pushDTO);
        return apiResult;
    }

}
